package com.tarena.luban.sentinel.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.tarena.luban.sentinel.handler.HelloBlockHandler;
import com.tarena.luban.sentinel.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/hello")
    @SentinelResource(
            value="sayHello",
            blockHandler = "blockHandler",
            blockHandlerClass= HelloBlockHandler.class,
            fallback="fallback",
            fallbackClass = HelloBlockHandler.class
            )
    public String sayHi(String name){
        System.out.println(helloService.getClass().getName());
        String result = helloService.sayHi(name);
        //可以拿到spring aop代理对象的,但是目前看来拿不到当前HelloController代理
        //Object proxy = AopContext.currentProxy();
        //System.out.println("当前类"+this.getClass().getName());
        //System.out.println("代理类"+proxy.getClass().getName());
        //int a=1/0;
        return result;
    }
    /*@SentinelResource(value="test",fallback="testFallback")
    public String test(String name){
        return "test "+name;
    }
    public String testFallback(String name,Throwable e){
        return "test资源被限流 "+name;
    }*/
    /**
     * [
     *  {
     *      "resource":"test",
     *      "grade":1,
     *      "count":0
     *  }
     * ]
     */
}
